﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DirectoryOpteration
{
    public partial class Form1 : Form
    {
        public static string dpath;
        string cs=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRoot_Click(object sender, EventArgs e)
        {
            if (valbase())
            {
                DateTime exmDate = dtExamDate.Value;
                string res = DirInfo.Class1.CreateRootDir(@cmbdrvName.Text + textBase.Text, exmDate);
                MessageBox.Show(res);
                if (res == "Directory Created")
                {
                    insDateRec();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (valchild())
            {
                DateTime exmDate = dtExamDate.Value;
                string res = DirInfo.Class1.CreateChildDir(@cmbdrvName.Text + textBase.Text, exmDate, txtExamTime.Text.ToString());
                MessageBox.Show(res);
                if (res == "Directory Created")
                {
                    insTimeRec();
                }
            }
        }

        private void txtExamTime_KeyUp(object sender, KeyEventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DirInfo.Class1.loadDrive(cmbdrvName);
            cmbdrvName.SelectedIndex = 0;
            DirectoryInfo dr = new DirectoryInfo(@cmbdrvName.Text);
            foreach (var dirname in dr.GetDirectories())
            {
                textBase.AutoCompleteCustomSource.Add(dirname.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (valsubchild())
            {
                DateTime exmDate = dtExamDate.Value;
                string res = DirInfo.Class1.CreateSubChildDir(@cmbdrvName.Text + textBase.Text, exmDate, txtExamTime.Text.ToString(), txtPaperCode.Text);
                MessageBox.Show(res);
                if (res == "Director Created")
                {
                    insPaperCodeRec();
                }
            }
        }

        private void cmbdrvName_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBase.Clear();
            textBase.AutoCompleteCustomSource.Clear();
            DirectoryInfo dr = new DirectoryInfo(@cmbdrvName.Text);
            if (dr.Exists)
            {
                foreach (var dirname in dr.GetDirectories())
                {
                    textBase.AutoCompleteCustomSource.Add(dirname.ToString());
                }
            }
            else
            {
                textBase.AutoCompleteCustomSource.Clear();
            }
        }
        public bool valbase()
        {
            if (cmbdrvName.SelectedIndex == -1)
            {
                MessageBox.Show("Select Drive Name");
                return false;
            }
            else if (textBase.Text == "")
            {
                MessageBox.Show("Enter Base Directory Name");
                textBase.Focus();
                return false;
            }
            return true;
        }
        public bool valchild()
        {
            if (cmbdrvName.SelectedIndex == -1)
            {
                MessageBox.Show("Select Drive Name");
                return false;
            }
            else if (textBase.Text == "")
            {
                MessageBox.Show("Enter Base Directory Name");
                textBase.Focus();
                return false;
            }
            else if (txtExamTime.Text == "")
            {
                 MessageBox.Show("Enter Exam Time");
                 txtExamTime.Focus();
                 return false;
            }
            return true;
        }
        public bool valsubchild()
        {
            if (cmbdrvName.SelectedIndex == -1)
            {
                MessageBox.Show("Select Drive Name");
                return false;
            }
            else if (textBase.Text == "")
            {
                MessageBox.Show("Enter Base Directory Name");
                textBase.Focus();
                return false;
            }
            else if (txtExamTime.Text == "")
            {
                MessageBox.Show("Enter Exam Time");
                txtExamTime.Focus();
                return false;
            }
            else if (txtPaperCode.Text == "")
            {
                MessageBox.Show("Enter Paper Code");
                txtPaperCode.Focus();
                return false;
            }
            return true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBase.Text == "")
            {
                MessageBox.Show("Enter Base Directory Name");
                textBase.Focus();
            }
            else
            {
                dpath = @cmbdrvName.Text + textBase.Text + @"\";
                DirectoryInfo dr = new DirectoryInfo(dpath);
                if (dr.Exists)
                {
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Base Folder Does Not Exist");
                }
            }
        }
        public void insDateRec()
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spInsDateRecord", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter edate = new SqlParameter("@edate", string.Format("{0:MM-dd-yyyy}", dtExamDate.Value));
                cmd.Parameters.Add(edate);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
        public void insTimeRec()
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spInsTimeRecord", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter edate = new SqlParameter("@edate", string.Format("{0:MM-dd-yyyy}", dtExamDate.Value));
                cmd.Parameters.Add(edate);
                SqlParameter etime = new SqlParameter("@etime", txtExamTime.Text);
                cmd.Parameters.Add(etime);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
        public void insPaperCodeRec()
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spInsPaperCodeRecord", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter edate = new SqlParameter("@edate", string.Format("{0:MM-dd-yyyy}", dtExamDate.Value));
                cmd.Parameters.Add(edate);
                SqlParameter etime = new SqlParameter("@etime", txtExamTime.Text);
                cmd.Parameters.Add(etime);
                SqlParameter epcode = new SqlParameter("@epcode", txtPaperCode.Text);
                cmd.Parameters.Add(epcode);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
}
